package com.gydz.gydzbaseframeworkboot.Common.utils.export;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class DateTimeUtil {

    public static void dateTime(HttpServletRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date();
        request.setAttribute("datemin",format.format(nowDate)+" 00:00:00");
        request.setAttribute("datemax",format.format(nowDate)+" 23:59:59");
    }

    public static void date(HttpServletRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date();
        request.setAttribute("datemin",format.format(nowDate));
        request.setAttribute("datemax",format.format(nowDate));
    }

    public static void main(String[] args) {
        String datemin = "",datemax = "";
        DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now().plus(-1, ChronoUnit.DAYS);
        System.out.println("today:"+format2.format(today));
        Date nowDate = new Date();
        int week = 3;//nowDate.getDay();
        System.out.println(week);
        if(week==5){
            datemin = format2.format(today);
            LocalDate max = today.plus(6, ChronoUnit.DAYS);
            datemax = format2.format(max);
        }else if(week==0){
            LocalDate min = today.minus(2, ChronoUnit.DAYS);
            LocalDate max = today.plus(4, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }else if(week==6){
            LocalDate min = today.minus(1, ChronoUnit.DAYS);
            LocalDate max = today.plus(5, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }else{
            LocalDate min = today.minus(7+week-5, ChronoUnit.DAYS);
            LocalDate max = today.plus(4-week, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }
        System.out.println("datemin:"+datemin+"|datemax:"+datemax);
    }

    public static void date5to4(HttpServletRequest request, int type) {
        String datemin = "",datemax = "";
        DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Date nowDate = new Date();
        LocalDate today = LocalDate.now();
        int week = nowDate.getDay(); //System.out.println(week);
        if(week==5){
            datemin = format2.format(today);
            LocalDate max = today.plus(6, ChronoUnit.DAYS);
            datemax = format2.format(max);
        }else if(week==0){
            LocalDate min = today.minus(2, ChronoUnit.DAYS);
            LocalDate max = today.plus(4, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }else if(week==6){
            LocalDate min = today.minus(1, ChronoUnit.DAYS);
            LocalDate max = today.plus(5, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }else{
            LocalDate min = today.minus(7+week-5, ChronoUnit.DAYS);
            LocalDate max = today.plus(4-week, ChronoUnit.DAYS);
            datemin = format2.format(min);
            datemax = format2.format(max);
        }
        //System.out.println("datemin:"+datemin+"|datemax:"+datemax);
        if(type == 1){
            request.setAttribute("datemin",datemin+" 00:00:00");
            request.setAttribute("datemax",datemax+" 23:59:59");
        }else{
            request.setAttribute("datemin",datemin);
            request.setAttribute("datemax",datemax);
        }
    }

    public static void date2(String datemin, String datemax) {
        if(StringUtils.isBlank(datemin) && StringUtils.isBlank(datemax)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(nowDate);
            c.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = c.getTime();
            datemin = format.format(yesterday);
            datemax = format.format(yesterday);
        }
    }
    public static void dateTime3(HttpServletRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, - 6);
        Date d = c.getTime();
        request.setAttribute("datemin",format.format(d));
        request.setAttribute("datemax",format.format(nowDate));
    }
    public static void dateTime1(HttpServletRequest request) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date();
        request.setAttribute("datemin",format.format(nowDate));
        request.setAttribute("datemax",format.format(nowDate));
    }
    public static String getNowTimeMin() {
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"+"00");
        return format.format(nowDate);
    }
    public static String getNowTimeMax() {
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"+"24");
        return format.format(nowDate);
    }

    public static void dateTime2(String datemin, String datemax) {
        if(StringUtils.isBlank(datemin) && StringUtils.isBlank(datemax)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(nowDate);
            c.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = c.getTime();
            datemin = format.format(yesterday)+" 00:00:00";
            datemax = format.format(yesterday)+" 23:59:59";
        }
    }

    public static String getNowTime() {
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(nowDate);
    }

    public static String getNowDate(String fm) {
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat(fm);
        return format.format(nowDate);
    }
    public static String getDateStr(String dateStr) throws ParseException {
        Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        String dateStr1= new SimpleDateFormat("yyyyMMddHH").format(date1);
        return dateStr1;
    }
    public static String getDateSelectmin(String dateStr) throws ParseException {
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        String dateStr1= new SimpleDateFormat("yyyyMMdd"+"00").format(date1);
        return dateStr1;
    }
    public static String getDateSelectmax(String dateStr) throws ParseException {
        Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        String dateStr1= new SimpleDateFormat("yyyyMMdd"+"24").format(date1);
        return dateStr1;
    }
    public static String getDate(Date date,String fm) {
        SimpleDateFormat format = new SimpleDateFormat(fm);
        return format.format(date);
    }

    public static int getMonthSpace(String date1, String date2)
            throws ParseException {

        int result = 0;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));

        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

        return result == 0 ? 1 : Math.abs(result);

    }

    public static int getDateSpace(String date1, String date2)
            throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calst = Calendar.getInstance();;
        Calendar caled = Calendar.getInstance();

        calst.setTime(sdf.parse(date1));
        caled.setTime(sdf.parse(date2));

        //设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数
        int days = ((int)(caled.getTime().getTime()/1000)-(int)(calst.getTime().getTime()/1000))/3600/24;

        return days;
    }

    public static String converttime(String s)
    {
        String result;
        SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        long It=new Long(s);
        Date date=new Date(It);
        result = simpleDateformat.format(date);
        return result;
    }

    public static String converttimesecond(String s)
    {
        String result;
        SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long It=new Long(s);
        Date date=new Date(It);
        result = simpleDateformat.format(date);
        return result;
    }

    public static long timetoStamp(String s)
    {
        long result;
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
            d = sdf.parse(s);
        } catch (ParseException pe) {
            System.out.println(pe.getMessage());
        }
        result=d.getTime();
        return result;
    }

    public static String returndate(long time) {
        Date date = new Date(time);
        int ii=date.getYear()+1900; //获取年份
        int i1=date.getMonth()+1;  //获取月份between 0 and 11
        int i2=date.getDate(); //获取日期
        String s1="";
        if(i2<10){
            s1="0"+i2;
        }
        else{
            s1=""+i2;
        }
        String s="";
        if(i1<10){
            s=s+ii+"0"+i1+s1;
        }
        else{
            s=s+ii+i1+s1;
        }
        return s;
    }

}
